<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Mockery\Exception;

class AdminController extends Controller
{

    //登录
    public function login(){


        try{
            $input = Request::only('admin_name', 'admin_password');
            $validator = Validator::make($input, [
                'admin_name' => 'required|max:16|min:4',
                'admin_password' => 'required|max:16|min:4',
            ]);
            if ($validator->fails()) {
                return $validator->messages();
            }


            $where['admin_name'] = $input['admin_name'];
            $where['admin_password'] = md5($input['admin_password']);

            $result = DB::table('admin')->where($where)->first();
            if($result){


                session_start();
                $_SESSION['isLogin'] =  $input['admin_name'];

                return redirect('/admin/index');
            }else{
                return redirect('/admin/login');
            }

        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }

    }

    //注销
    public function logout(){

        try{

            session_destroy();
            return view('/admin/login');

        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }

    }

    //添加管理员
    public  function  addAdmin(){

        try{

            $admin_name = $_SESSION['isLogin'];
            $power = DB::table('admin')->where('admin_name','=',$admin_name)->pluck('admin_power');

            if($power[0] !== 1){
                return  '权限不足';
            }else{
                $data = Request::only('admin_name', 'admin_password');

                $validator = Validator::make($data, [
                    'admin_name' => 'required|max:16|min:4',
                    'admin_password' => 'required|max:16|min:4',
                ]);

                if ($validator->fails()) {
                    return $validator->messages();
                }

                $check = DB::table('admin')->where('admin_name','=',$data['admin_name'])->first();
                if($check){

                    return json_encode(['msg'=>0,'data' => '该管理员名已存在']);
                }

                $data['admin_password'] = md5($data['admin_password']);
                $result = DB::table('admin')->insert($data);

                if($result){

                    return redirect('/admin/getAdminList');
                }
            }
        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }
    }
    //修改管理员密码
    public  function  updateAdmin(){

        try{

            $admin_name = $_SESSION['isLogin'];
            $power = DB::table('admin')->where('admin_name','=',$admin_name)->pluck('admin_power');
            if($power[0] !== 1){
                return '权限不足';
            }else{
                $data = Request::only('admin_name','admin_password');
                $admin_password= md5($data['admin_password']);

                var_dump($data);
                $result  = DB::table('admin')->where('admin_name','=',$data['admin_name'])->update(['admin_password' => $admin_password]);
                if($result){

                    return redirect('/admin/getAdminList');
                }
            }
        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>1,'data' => $info]);
            exit;
        }
    }
    //删除管理员
    public  function  deleteAdmin(){

        try{

            $admin_name = $_SESSION['isLogin'];
            $power = DB::table('admin')->where('admin_name','=',$admin_name)->pluck('admin_power');

            if($power[0] !== 1){
                return   '权限不足';
            }
            else{
                $data = Request::only('admin_id');
                $result  = DB::table('admin')->where('admin_id','=',$data['admin_id'])->delete();
                if($result){

                   return redirect('/admin/getAdminList');
                }
            }
        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }
    }
    //获取管理员列表
    public  function  getAdminList(){

        try{

            $admin_name = $_SESSION['isLogin'];
            $power = DB::table('admin')->where('admin_name','=',$admin_name)->pluck('admin_power');

            if($power[0] !== 1){
                return '权限不足';
            }else{

                $results = DB::table('admin')->paginate(10);

                //return json_encode(['msg'=>1,'data' => $result,'totalPage'=>$totalPage]);
                return view('Admin.adminlist',['results'=> $results]);
            }
        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }
    }
}
